﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RG.Common.WPF.Controls
{
    /// <summary>
    /// Interaction logic for PageScroller.xaml
    /// </summary>
    public partial class PageScroller 
    {
        public PageScroller()
        {
            InitializeComponent();

        }

        #region decimal Value
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(decimal), typeof(PageScroller),
                                            new FrameworkPropertyMetadata((decimal)0, new PropertyChangedCallback(OnValueChanged)));

        public decimal Value
        {
            set { SetValue(ValueProperty, value); }
            get { return (decimal)GetValue(ValueProperty); }
        }
        #endregion

        #region decimal MaxValue

        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(decimal?), typeof(PageScroller),
                                        new PropertyMetadata(null));

        public decimal? MaxValue
        {
            set { SetValue(MaxValueProperty, value); }
            get { return (decimal?)GetValue(MaxValueProperty); }
        }
        #endregion

        #region decimal MinValue

        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(decimal?), typeof(PageScroller),
                                        new PropertyMetadata(null));

        public decimal? MinValue
        {
            set { SetValue(MinValueProperty, value); }
            get { return (decimal?)GetValue(MinValueProperty); }
        }
        #endregion

        #region decimal Increment

        public static readonly DependencyProperty IncrementProperty =
            DependencyProperty.Register("Increment", typeof(decimal), typeof(PageScroller),
                                        new PropertyMetadata((decimal)1));

        public decimal Increment
        {
            set { SetValue(IncrementProperty, value); }
            get { return (decimal)GetValue(IncrementProperty); }
        }
        #endregion

        #region bool ReverseButtons

        public static readonly DependencyProperty ReverseButtonsProperty =
            DependencyProperty.Register("ReverseButtons", typeof(bool), typeof(PageScroller),
                                        new PropertyMetadata(false));

        public bool ReverseButtons
        {
            set { SetValue(ReverseButtonsProperty, value); }
            get { return (bool)GetValue(ReverseButtonsProperty); }
        }
        #endregion


        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d.GetValue(MaxValueProperty) != null &&
               (decimal)e.NewValue > (decimal)(d.GetValue(MaxValueProperty)))
                d.SetValue(ValueProperty, (decimal)d.GetValue(MaxValueProperty));
            if (d.GetValue(MinValueProperty) != null &&
               (decimal)e.NewValue < (decimal)(d.GetValue(MinValueProperty)))
                d.SetValue(ValueProperty, (decimal)d.GetValue(MinValueProperty));
        }

        private void UpButton_Click(object sender, RoutedEventArgs e)
        {
            Value = ReverseButtons ? Value - Increment : Value + Increment;
        }

        private void DownButton_Click(object sender, RoutedEventArgs e)
        {
            Value = ReverseButtons ? Value + Increment : Value - Increment;
        }

        private void CurValue_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Up)
                Value = ReverseButtons ? Value - Increment : Value + Increment;
            if (e.Key == Key.Down)
                Value = ReverseButtons ? Value + Increment : Value - Increment;
        }
    }
}
